import { getSeriesData } from "@@/apis/video/series"
import { reactive, ref } from "vue"

export function useTable() {
  const tableData = ref<any[]>([])
  const searchParams = reactive({
    seriesName: ""
  })

  const currentPage = ref(1)
  const pageSize = ref(15)
  const total = ref(0)

  interface SeriesDataResponse {
    data?: {
      list: any[]
      total: number
    }
  }

  const fetchData = async () => {
    const params = {
      seriesName: searchParams.seriesName,
      page: currentPage.value,
      pageSize: pageSize.value
    }
    const res = await getSeriesData(params) as SeriesDataResponse
    tableData.value = res.data?.list || []
    total.value = res.data?.total || 0
  }

  const handlePageChange = (page: number) => {
    currentPage.value = page
    fetchData()
  }

  return {
    tableData,
    searchParams,
    currentPage,
    pageSize,
    total,
    fetchData,
    handlePageChange
  }
}
